package com.glsc.ngateway.platform.utils;

import org.apache.logging.log4j.util.Strings;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author xuchang
 */
public class StringUtils {
    public static String getOrEmpty(@Nullable String str) {
        return Strings.isNotBlank(str) ? str : Strings.EMPTY;
    }

    public static String getOrEmpty(@Nullable LocalDate date) {
        return Objects.nonNull(date) ? date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) : Strings.EMPTY;
    }
    public static String getOrEmpty(@Nullable LocalDateTime date) {
        return Objects.nonNull(date) ? date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) : Strings.EMPTY;
    }
    public static String getOrEmpty(List<String> list) {
        return CollectionUtils.isEmpty(list) ? Strings.EMPTY : String.join(",", list);
    }

    public static <T> String getOrEmpty(List<T> list, @NotNull Function<T, String> func) {
        return CollectionUtils.isEmpty(list) ? Strings.EMPTY : list.stream().map(func).collect(Collectors.joining(","));
    }

    public static String getOrEmpty(@Nullable Integer num) {
        return Objects.isNull(num) ? Strings.EMPTY : String.valueOf(num);
    }

    public static String getOrEmpty(@Nullable Long l) {
        return Objects.isNull(l) ? Strings.EMPTY : String.valueOf(l);
    }
}
